package com.gdut.ui;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;

public class ManageJFrame extends JFrame implements MouseListener {
    DefaultListModel<String> listModel = new DefaultListModel<>();
    JList<String> jList = new JList<>(listModel);
    JScrollPane jScrollPane = new JScrollPane(jList);

    JPanel Panel = new JPanel();
    JButton uploadButton = new JButton("上传图片");
    JButton deleteButton = new JButton("删除选中的图片");
    JButton refreshButton = new JButton("刷新列表");

    public ManageJFrame() {
        initial();
        this.setVisible(true);
    }

    public void initial() {
        this.setSize(450, 400);
        this.setLayout(new BorderLayout());
        this.setTitle("文件管理系统");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Panel.add(uploadButton);
        Panel.add(deleteButton);
        Panel.add(refreshButton);
        this.add(jScrollPane, BorderLayout.CENTER);
        refresh(listModel);
        this.add(Panel, BorderLayout.NORTH);
        uploadButton.addMouseListener(this);
        deleteButton.addMouseListener(this);
        refreshButton.addMouseListener(this);
        jList.addMouseListener(this);
    }

    private void refresh(DefaultListModel<String> model) {
        model.clear();
        File folder = new File(LoginJFrame.FOlDER_NAME);
        if (folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles((_, name) -> name.toLowerCase().matches(".*\\.(jpg|png)$"));
            if (files != null) {
                for (File file : files) {
                    model.addElement(file.getName());
                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == uploadButton) {
            load();
        } else if (e.getSource() == deleteButton) {
            delete();
        } else if (e.getSource() == refreshButton) {
            refresh(listModel);
        } else if (e.getClickCount() == 2) {
            open();
        }
    }

    public void load() {
        JFileChooser jFileChooser = new JFileChooser();
        FileNameExtensionFilter fileNameExtensionFilter = new FileNameExtensionFilter("图片", "jpg", "png");
        jFileChooser.setFileFilter(fileNameExtensionFilter);
        if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            File select = jFileChooser.getSelectedFile();
            String fileSouses = select.toString();
            String fileTarget = (LoginJFrame.FOlDER_NAME + "\\" + select.getName());
            try {
                copy(fileSouses, fileTarget);
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, "上传失败");
            }
        }
        refresh(listModel);
    }

    public void copy(String souses, String target) throws IOException {
        FileInputStream fis = new FileInputStream(souses);
        FileOutputStream fos = new FileOutputStream(target);
        byte[] b = new byte[1024 * 1024 * 2];
        int len;
        while ((len = fis.read(b)) != -1) {
            fos.write(b, 0, len);
        }
        fis.close();
        fos.close();
    }

    public void delete() {
        String select = jList.getSelectedValue();
        if (select != null) {
            int choice = JOptionPane.showConfirmDialog(this, "是否确定删除" + select);
            if (choice == JOptionPane.YES_OPTION) {
                File file = new File(LoginJFrame.FOlDER_NAME, select);
                if (file.delete()) {
                    refresh(listModel);
                } else {
                    JOptionPane.showMessageDialog(this, "删除失败");
                }
            }
        }
    }

    public void open() {
        String select = jList.getSelectedValue();
        if (select != null) {
            File file = new File(LoginJFrame.FOlDER_NAME, select);
            try {
                Desktop.getDesktop().open(file);
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, "打开文件失败");
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
